#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ビューレット機能

Mikan Python Web Framework
"""

__author__ = "K.Sonohara"
__status__ = "develop"
__version__ = "0.0.0_23"
__date__    = "2013/07/07"
__license__ = 'LGPL'

# ------------------------------------------------------------
## インポート
# Python
import platform
import socket
import traceback

# ESiPC
from esipc import ESiPCProperty

# Mikan
from mikan.viewlet import *
from mikan.core import MIKAN

# ------------------------------------------------------------
## Mikan メニューバー ビューレットクラス
#
class MikanIconViewlet(MikanImgViewlet):

	def __init__(self, parent, alt, src, clazz=None):
		super(MikanIconViewlet, self).__init__(parent, src, alt, clazz)

	def perform(self):
		if super(MikanIconViewlet, self).perform() == False:
			return False

		return True

# ------------------------------------------------------------
## Mikan メニューバー ビューレットクラス
#
class MikanHeaderLogoViewlet(MikanImgViewlet):

	def __init__(self, parent):
		super(MikanHeaderLogoViewlet, self).__init__(parent, alt=u"ロゴ", src=parent.request.imageurl + parent.request.sitelogo)

	def perform(self):
		if super(MikanHeaderLogoViewlet, self).perform() == False:
			return False

		return True

# ------------------------------------------------------------
## Mikan メニューバー ビューレットクラス
#
class MikanHeaderHomeViewlet(MikanAViewlet):

	def __init__(self, parent):
		super(MikanHeaderHomeViewlet, self).__init__(parent, href=parent.request.wsgiurl + "/", nodevalue= parent.request.sitetitle)
		self.clazz = "header_button"

	def perform(self):
		MikanIconViewlet(self, u"□", self.request.imageurl + "site.png", "icon_small")

		if super(MikanHeaderHomeViewlet, self).perform() == False:
			return False

		return True

# ------------------------------------------------------------
## Mikan メニューバー ビューレットクラス
#
class MikanHeaderButtonViewlet(MikanDivViewlet):

	def get_buttons(self):
		return self.FButton

	def __init__(self, parent):
		super(MikanHeaderButtonViewlet, self).__init__(parent)

		self.FButton = []

	def perform(self):
		for b in self.buttons:
			s = MikanSpanViewlet(self)
			a = MikanAViewlet(s, b.url, nodevalue=b.title)
			a.clazz = "header_button"
			if b.icon:
				MikanIconViewlet(a, alt=u"□", src=b.icon, clazz="icon_small")

		if super(MikanHeaderButtonViewlet, self).perform() == False:
			return False

		return True

	# ----------------------------------------------------
	## Property
	buttons = ESiPCProperty(get_buttons, None, None)

# ------------------------------------------------------------
## Mikan メニューバー ビューレットクラス
#
class MikanMenuViewlet(MikanTableViewlet):

	def set_mode(self, value):
		self.FMode = value

	def get_mode(self):
		return self.FMode

	def set_menus(self, value):
		self.FMenus = value

	def get_menus(self):
		return self.FMenus

	def get_table(self):
		return self.FTable

	def __init__(self, parent, menus, mode="toggle"):
		super(MikanMenuViewlet, self).__init__(parent)

		self.clazz = "menu_tab"

		self.FMenus = menus
		self.FMode = mode

	def build_menus(self, menus, element, tag="div", c="menu_popupmenu", tag_td="td"):
		if (element == None):
			return

		if (menus != None):
			d = MikanHTMLElementViewlet(element, tag)
			d.clazz = c

			for m in menus:
				if (m.url == None):
					th = MikanThViewlet(d)
					th.clazz = "menu_title"
					if (m.icon != None):
						ic = "icon_small"
						if (self.mode == "toggle"):
							ic = "icon_middle"
						img = MikanImgViewlet(th, m.title, m.icon, ic)
					th.FValue = m.title
				else:
					td = MikanHTMLElementViewlet(d, tag_td)
					td.clazz = "menu"
					if (self.mode == "toggle"):
						td.clazz = "menu_toggle"
#					td.FOnMouseOut = "hidePopupMenu(this,\'menu_popupmenu\');"
#					td.FOnMouseOver = "showPopupMenu(this,\'menu_popupmenu\');"
					a = MikanAViewlet(td,  href=m.url, target=m.target)
					a.clazz = "menu_button"
					if (self.mode == "toggle"):
						a.clazz = "menu_toggle"
					if (m.icon != None):
						ic = "icon_small"
						if (self.mode == "toggle"):
							ic = "icon_middle"
						img = MikanImgViewlet(a, alt=m.title, src=m.icon, clazz=ic)
					if (self.mode == "toggle"):
						MikanBrViewlet(a)
					title = MikanTextViewlet(a, m.title)

					if (m.menus != None):
						if (len(m.menus) != 0):
							MikanBrViewlet(td).clazz = "menu"
							self.build_menus(m.menus, td, "div", "menu_popupmenu", "div")

	def perform(self):
		self.build_menus(self.menus, self, "tr", "menu")

		return super(MikanMenuViewlet, self).perform()

	# ----------------------------------------------------
	## Property
	mode = ESiPCProperty(get_mode, set_mode, None)
	menus = ESiPCProperty(get_menus, set_menus, None)
	table = ESiPCProperty(get_table, None, None)


# ------------------------------------------------------------
## Mikan メニューバー ビューレットクラス
#
class MikanClockViewlet(MikanDivViewlet):

	def __init__(self, parent):
		super(MikanClockViewlet, self).__init__(parent)

		div = MikanDivViewlet(self, "-")
		div.id = "clock"

# ------------------------------------------------------------
## Mikan メニューバー ビューレットクラス
#
class MikanDateViewlet(MikanDivViewlet):

	def __init__(self, parent):
		super(MikanDateViewlet, self).__init__(parent)

		div = MikanDivViewlet(self, "-")
		div.id ="date"

# ------------------------------------------------------------
## Mikan メニューバー ビューレットクラス
#
class AbstractMikanInfoViewlet(MikanTableViewlet):

	def __init__(self, parent):
		super(AbstractMikanInfoViewlet, self).__init__(parent)
		self.clazz = "item_list"

	def addRow(self, tbody, label, value, even):
		tr = MikanTrViewlet(tbody)
		tr.clazz = "item_row"
		th = MikanThViewlet(tr, nodevalue=label)
		th.clazz= "item_label"
		td = MikanTdViewlet(tr, nodevalue=value)
		if even:
			td.clazz = "item_cell"
		else:
			td.clazz = "item_cell2"

# ------------------------------------------------------------
## Mikan メニューバー ビューレットクラス
#
class MikanSiteInfoViewlet(AbstractMikanInfoViewlet):

	def __init__(self, parent):
		super(MikanSiteInfoViewlet, self).__init__(parent)
		self.clazz = "item_list"

	def perform(self):

		tbody = MikanTbodyViewlet(self)

		self.addRow(tbody, u"サイト名", "%s" % (self.request.sitetitle), False)
		self.addRow(tbody, u"サイト詳細", "%s" % (self.request.sitedescription), True)
		self.addRow(tbody, u"サイトバージョン", "%s" % (self.request.siteversion), False)
		self.addRow(tbody, u"サイト更新日", "%s" % (self.request.sitedate), True)
		self.addRow(tbody, u"サイトシステム名", "%s" % (self.request.sitesystem), False)
		self.addRow(tbody, u"サイト著作権", "%s" % (self.request.sitecopyright), True)
		self.addRow(tbody, u"サイトオーナ", "%s" % (self.request.siteauther), False)

		if super(MikanSiteInfoViewlet, self).perform() == False:
			return False

		return True

# ------------------------------------------------------------
## Mikan メニューバー ビューレットクラス
#
class MikanPythonInfoViewlet(AbstractMikanInfoViewlet):

	def __init__(self, parent):
		super(MikanPythonInfoViewlet, self).__init__(parent)
		self.clazz = "item_list"

	def perform(self):

		tbody = MikanTbodyViewlet(self)

		self.addRow(tbody, u"Python", "%s %s" % (platform.python_implementation(), platform.python_version()), False)
		self.addRow(tbody, u"Web Framework", "%s %s" % (MIKAN.MIKAN_NAME, MIKAN.MIKAN_VERSION), True)
		self.addRow(tbody, u"OS", "%s %s" % (platform.system(), platform.release()), False)
		self.addRow(tbody, u"マシンタイプ", platform.machine(), False)
		self.addRow(tbody, u"ホスト名", platform.node(), True)
		try:
			self.addRow(tbody, u"IPアドレス", socket.gethostbyname(socket.gethostname()), False)
		except:
			pass
		self.addRow(tbody, u"プロセッサタイプ", platform.processor(), True)
		self.addRow(tbody, u"サーバー名", self.request.serversoftware, False)

		try:
			db = self.request.db

			try:
				self.addRow(tbody, u"データベース接続タイプ", "%s" % (db.name), True)
			except:
				pass

			try:
				self.addRow(tbody, u"データベースバージョン", "%s" % (db.version), False)
			except:
				pass
		except:
			pass

		if super(MikanPythonInfoViewlet, self).perform() == False:
			return False

		return True

# ------------------------------------------------------------
## Mikan メニューバー ビューレットクラス
#
class MikanRequestInfoViewlet(AbstractMikanInfoViewlet):

	def __init__(self, parent):
		super(MikanRequestInfoViewlet, self).__init__(parent)
		self.clazz = "item_list"

	def perform(self):

		tbody = MikanTbodyViewlet(self)

		self.addRow(tbody, u"サーバープロトコル", self.request.serverprotocol, False)
		self.addRow(tbody, u"CGI", self.request.gatewayinterface, True)
		self.addRow(tbody, u"ロケール", self.request.locale.__str__(), False)
		self.addRow(tbody, u"タイムゾーン", self.request.timezone.__str__(), True)

		even = False
		for k in sorted(self.request.environ.keys()):
			self.addRow(tbody, k, self.request.environ[k], even)
			even = not even

		if super(MikanRequestInfoViewlet, self).perform() == False:
			return False

		return True
